package org.batatasframework.values;

import java.util.ArrayList;
import java.util.List;

import cn.bonoon.kernel.util.StringHelper;

/**
 * 坐标值
 * @author jackson
 *
 */
public class CoordinateValue {

	public static List<List<CoordinateValue>> toArray(ValueGetter getter){
		List<List<CoordinateValue>> cvsList = new ArrayList<>();
		if(null == getter) return cvsList;
		String[] strs = getter.getString().split(";");
		for(String str : strs){
			cvsList.add(_toList(str.split(" ")));
		}
		return cvsList;
	}
	
	public static List<CoordinateValue> toList(String str){
		List<CoordinateValue> list = new ArrayList<>();
		if(null == str) return list;
		String[] strs = str.split(";");
		for(String st : strs){
			list.add(valueOf(st));
		}
		return list;
	}
	
	private static List<CoordinateValue> _toList(String[] strs) {
		if(strs.length > 0){
			List<CoordinateValue> list = new ArrayList<>();
			for(String str : strs){
				list.add(valueOf(str));
			}
			return list;
		}
		return null;
	}

	public static CoordinateValue valueOf(String str){
		return valueOf(str.split(","));
	}
	
	public static CoordinateValue valueOf(ValueGetter getter){
		return valueOf(getter.getString());
	}
	
	public static CoordinateValue valueOf(ValueReader reader, String name){
		return valueOf(reader.getString(name));
	}
	
	public static List<CoordinateValue> toList(ValueReader reader, String name){
		return _toList(reader.getString(name).split("|"));
	}
	
	private static CoordinateValue valueOf(String[] value){
		if(value.length > 1){
			return new CoordinateValue(value[0], value[1]);
		}
		return null;
	}
	
	/**
	 * 经度
	 */
	private final double longitude;
	
	/**
	 * 纬度
	 */
	private final double latitude;

	public CoordinateValue(String value1, String value2){
		this.latitude  = StringHelper.todouble(value1);
		this.longitude = StringHelper.todouble(value2);
	}
	
	public double getLatitude() {
		return latitude;
	}
	
	public double getLongitude() {
		return longitude;
	}
}
